package per.sunmes.lesrb.font;

import java.util.HashMap;
import java.util.Map;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;

import per.sunmes.lesrb.manager.RM;

/** LabelManager */
public class LM {
	private static LM _i;
	public static TextureFilter defaultFontFilter = TextureFilter.Linear;

	private static LM I() {
		if (_i == null) {
			_i = new LM();
		}
		return _i;
	}

	public static void dispose() {
		if (_i != null) {
			_i._dispose();
			_i = null;
		}
	}

	public static FreeTypeFontGenerator createGenerator(String fontName) {
		return new FreeTypeFontGenerator(RM.getFile(fontName));
	}

	public static BitmapFont createCacheFont(String fontName, String cacheName, FreeTypeFontParameter param) {
		return I()._createCacheFont(fontName, cacheName, param).font;
	}

	public static BitmapFont getCacheFont(String cacheName) {
		FreeFontFlyBuildStruct fff = I().caches.get(cacheName);
		if (fff == null) {
			return null;
		} else {
			return fff.font;
		}
	}

	public static void disposeCachedFont(String cacheName) {
		FreeFontFlyBuildStruct fbs = I().caches.remove(cacheName);
		if (fbs != null) {
			fbs.generator.dispose();
			fbs.font.dispose();
		}
	}

	public static BitmapFont createCacheFont(String fontName, String cacheName, int fontSize) {
		FreeTypeFontParameter param = new FreeTypeFontParameter();
		param.color = Color.WHITE;
		param.incremental = true;
		param.size = fontSize;
		param.minFilter = defaultFontFilter;
		param.magFilter = defaultFontFilter;
		return createCacheFont(fontName, cacheName, param);
	}

	public static BitmapFont createCacheFontWithBorder(String fontName, String cacheName, int fontSize, int borderSize,
			Color borderColor) {
		FreeTypeFontParameter param = new FreeTypeFontParameter();
		param.color = Color.WHITE;
		param.incremental = true;
		param.size = fontSize;
		param.minFilter = defaultFontFilter;
		param.magFilter = defaultFontFilter;
		param.borderWidth = borderSize;
		param.borderColor = borderColor;
		return createCacheFont(fontName, cacheName, param);
	}

	public static BitmapFont createCacheFontWithShadow(String fontName, String cacheName, int fontSize,
			Color shadowColor) {
		FreeTypeFontParameter param = new FreeTypeFontParameter();
		param.color = Color.WHITE;
		param.incremental = true;
		param.size = fontSize;
		param.minFilter = defaultFontFilter;
		param.magFilter = defaultFontFilter;
		param.shadowColor = shadowColor;
		param.shadowOffsetX = 1;
		param.shadowOffsetY = 1;
		return createCacheFont(fontName, cacheName, param);
	}

	/////////////
	Map<String, FreeFontFlyBuildStruct> caches = new HashMap<String, FreeFontFlyBuildStruct>();

	private FreeFontFlyBuildStruct _createCacheFont(String fontName, String cacheName, FreeTypeFontParameter param) {
		if (caches.containsKey(cacheName)) {
			throw new RuntimeException("Font cache ,name[" + cacheName + "] was already cached!");
		}
		FreeFontFlyBuildStruct fbs = new FreeFontFlyBuildStruct();
		fbs.generator = createGenerator(fontName);
		fbs.param = param;
		fbs.cacheName = cacheName;
		fbs.font = fbs.generator.generateFont(param);

		caches.put(cacheName, fbs);
		return fbs;
	}

	private void _dispose() {
		for (FreeFontFlyBuildStruct fbs : caches.values()) {
			fbs.generator.dispose();
			fbs.font.dispose();
		}
		caches.clear();
	}

}
